package com.example.vpn_demo.http2

import android.util.Log
import okhttp3.*
import okio.Buffer
import java.io.IOException
import java.util.concurrent.TimeUnit

class Http2Connection(
    private val client: OkHttpClient,
    private val listener: Http2EventListener
) {
    companion object {
        private const val TAG = "Http2Connection"
    }

    fun sendRequest(request: Request) {
        try {
            client.newCall(request).enqueue(object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    listener.onError(e)
                }

                override fun onResponse(call: Call, response: Response) {
                    listener.onResponse(response)
                }
            })
        } catch (e: Exception) {
            Log.e(TAG, "Error sending HTTP/2 request", e)
            listener.onError(e)
        }
    }
}

interface Http2EventListener {
    fun onResponse(response: Response)
    fun onError(error: Throwable)
} 